﻿using System.Drawing;
using System.IO;

// MES_SystemUI.Helpers.ImageHelper

namespace MES_SystemUI.Helpers
{
    public static class ImageHelper
    {
        /// <summary>
        /// 图片转换成字节数组
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToByteArray(Image image)
        {
            if (image == null) return null;
            using (var memoryStream = new MemoryStream())
            {
                image.Save(memoryStream, image.RawFormat);
                return memoryStream.ToArray();
            }
        }

        /// <summary>
        /// 字节数组转换成图片
        /// </summary>
        /// <param name="byteArray"></param>
        /// <returns></returns>
        public static Image ByteArrayToImage(byte[] byteArray)
        {
            if (byteArray == null) return null;
            using (var memoryStream = new MemoryStream(byteArray))
            {
                return Image.FromStream(memoryStream);
            }
        }
    }
}
